package com.buka.communicate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buka.communicate.R;
import com.buka.communicate.adapter.CircleBgAdapter;
import com.buka.communicate.adapter.GiftBgAdapter;
import com.buka.communicate.adapter.GiftNumAdapter;
import com.buka.communicate.adapter.SkillBgAdapter;
import com.buka.communicate.adapter.TagAdapter_;
import com.buka.communicate.views.BounceScrollView;
import com.buka.communicate.views.MyRecylerView;
import com.facebook.drawee.view.SimpleDraweeView;

import net.neiquan.applibrary.base.BaseActivity;
import net.neiquan.applibrary.base.MyBaseAdapter;
import net.neiquan.applibrary.flowtag.FlowTagLayout;
import net.neiquan.applibrary.flowtag.OnTagSelectListener;
import net.neiquan.applibrary.wight.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/21 0021.
 */

public class PersonalPageActivity extends BaseActivity {
    @InjectView(R.id.mHeadRv)
    RelativeLayout mHeadRv;
    @InjectView(R.id.mBounceView)
    BounceScrollView mBounceView;
    @InjectView(R.id.mSkillLogo)
    ImageView mSkillLogo;
    @InjectView(R.id.mGiftLogo)
    ImageView mGiftLogo;
    @InjectView(R.id.mTagFlowLayout)
    FlowTagLayout mTagFlowLayout;
    @InjectView(R.id.mHeadImg)
    SimpleDraweeView mHeadImg;
    @InjectView(R.id.mHeadImg1)
    SimpleDraweeView mHeadImg1;
    @InjectView(R.id.mHeadImg2)
    SimpleDraweeView mHeadImg2;
    @InjectView(R.id.mHeadImg3)
    SimpleDraweeView mHeadImg3;
    @InjectView(R.id.mFamilyHeadImg)
    SimpleDraweeView mFamilyHeadImg;
    @InjectView(R.id.mCircleRecylerView)
    MyRecylerView mCircleRecylerView;
    @InjectView(R.id.mGiftRecylerView)
    MyRecylerView mGiftRecylerView;
    @InjectView(R.id.mSkillRecylerView)
    MyRecylerView mSkillRecylerView;
    @InjectView(R.id.mFamilyTv)
    TextView mFamilyTv;
    @InjectView(R.id.friendOprate)
    ImageView friendOprate;
    @InjectView(R.id.friendOprate)
    ImageView addFriendLogo;
    @InjectView(R.id.mLL2)
    LinearLayout mLL2;
    @InjectView(R.id.mLL1)
    LinearLayout mLL1;
    @InjectView(R.id.mLL3)
    LinearLayout mLL3;
    @InjectView(R.id.backFL)
    FrameLayout backFL;
    @InjectView(R.id.familyDetail)
    LinearLayout familyDetail;
    @InjectView(R.id.friendCircleLL)
    LinearLayout friendCircleLL;
    private TagAdapter_<String> adapter;

    @Override
    public int getRootViewId() {
        return R.layout.activity_personal_page;
    }

    @Override
    public void setViews() {
        Spanned spanned = Html.fromHtml("我正在<font color='#00ffff'>[快乐大本营]</font>与朋友聊天");
        mFamilyTv.setText(spanned);
        mHeadImg.setImageURI("http://img2.imgtn.bdimg.com/it/u=266057472,3867980612&fm=27&gp=0.jpg");
        mHeadImg1.setImageURI("http://img2.imgtn.bdimg.com/it/u=266057472,3867980612&fm=27&gp=0.jpg");
        mHeadImg2.setImageURI("http://img2.imgtn.bdimg.com/it/u=266057472,3867980612&fm=27&gp=0.jpg");
        mHeadImg3.setImageURI("http://img2.imgtn.bdimg.com/it/u=266057472,3867980612&fm=27&gp=0.jpg");
        mFamilyHeadImg.setImageURI("http://img2.imgtn.bdimg.com/it/u=266057472,3867980612&fm=27&gp=0.jpg");
        initTagRecylerView();
        initRecylerView();
    }

    @Override
    public void initData() {

    }

    private void initRecylerView() {
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mCircleRecylerView.setLayoutManager(layoutManager);
        CircleBgAdapter circleBgAdapter = new CircleBgAdapter(this, null);
        mCircleRecylerView.setAdapter(circleBgAdapter);
        mCircleRecylerView.setNestedScrollingEnabled(false);
        circleBgAdapter.setOnItemClickListener(new MyBaseAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                startActivity(new Intent(PersonalPageActivity.this, MyCircleActivity.class));
            }
        });

        LinearLayoutManager layoutManager2 =
                new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mGiftRecylerView.setLayoutManager(layoutManager2);
        mGiftRecylerView.setAdapter(new GiftBgAdapter(this, null));
        mGiftRecylerView.setNestedScrollingEnabled(false);

        StaggeredGridLayoutManager layoutManager3 = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager3.setOrientation(StaggeredGridLayoutManager.VERTICAL);
        mSkillRecylerView.setLayoutManager(layoutManager3);
        mSkillRecylerView.setAdapter(new SkillBgAdapter(this, null));
        mSkillRecylerView.setNestedScrollingEnabled(false);
    }

    private void initTagRecylerView() {
        List<String> strings = new ArrayList<>();
        strings.add("白领");
//        strings.add("宇宙第一帅");
//        strings.add("文艺");
//        strings.add("极品吃货");
//        strings.add("萌萌哒");
//        strings.add("萌萌哒");
//        strings.add("萌萌哒");
//        strings.add("萌萌哒");
        strings.add("+添加印象");
        mTagFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        adapter = new TagAdapter_<String>(this);
        mTagFlowLayout.setAdapter(adapter);
        adapter.clearAndAddAll(strings);
        mTagFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                startActivity(new Intent(PersonalPageActivity.this, ImpressTagActivity.class));
//                for (int i = 0; i < selectedList.size(); i++) {
//                    AppLog.e("==========setOnTagSelectListener===========" + selectedList.get(i));
//                    Search data = ((Search) parent.getAdapter().getItem(selectedList.get(i)));
//                    Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
//                    intent.putExtra("search", data.getTitle());
//                    startActivity(intent);
//                }
            }
        });
    }

    @OnClick({R.id.friendOprate, R.id.addFriendLogo, R.id.backFL, R.id.familyDetail, R.id.friendCircleLL, R.id.mGiftLogo,R.id.mSkillLogo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.friendOprate:
                showFriendOptPopWindow();
                break;
            case R.id.addFriendLogo:
                break;
            case R.id.backFL:
                finish();
                break;
            case R.id.familyDetail:
                startActivity(new Intent(PersonalPageActivity.this, FamilyDetailActivity.class));
                break;
            case R.id.friendCircleLL:
                startActivity(new Intent(PersonalPageActivity.this, MyCircleActivity.class));
                break;
            case R.id.mGiftLogo:
                startActivity(new Intent(PersonalPageActivity.this, GiftActivity.class));
                break;
            case R.id.mSkillLogo:
                startActivity(new Intent(PersonalPageActivity.this, SkillActivity.class));
                break;
        }
    }

    private void showFriendOptPopWindow() {
        CommonPopupWindow popupWindow = new CommonPopupWindow.Builder(this)
                //设置PopupWindow布局
                .setView(R.layout.friend_opt_pop)
                //设置宽高
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)
                //设置动画
                .setAnimationStyle(R.style.Animation_right)
                //设置背景颜色，取值范围0.0f-1.0f 值越小越暗 1.0f为透明
                .setBackGroundLevel(0.5f)
                //设置PopupWindow里的子View及点击事件
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(final PopupWindow popupWindow, View view, int layoutResId) {
                    }
                })
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();
        //弹出PopupWindow
        popupWindow.showAsDropDown(friendOprate, 0, 10);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
